package com.xjj.dbfs;

public class M0130SurroundedRegions {

    boolean[][] checked;

    int[] xArr = {0, 0, 1, -1};

    int[] yArr = {1, -1, 0, 0};

    public void solve(char[][] board) {
        checked = new boolean[board.length][board[0].length];
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                // 从边上进去岛屿
                boolean isBoundary = i == 0 || j == 0 || i == board.length - 1 || j == board[0].length -1;
                if (isBoundary && board[i][j] == 'O' && !checked[i][j]) {
                    visitGrid(i, j, board);
                }
            }
        }
        for (int i = 0; i < checked.length; i++) {
            for (int j = 0; j < checked[i].length; j++) {
                if (!checked[i][j]) {
                    board[i][j] = 'X';
                }
            }
        }
    }


    private void visitGrid(int i, int j, char[][] grid) {
        if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] != 'O' || checked[i][j]) {
            return;
        }
        checked[i][j] = true;
        for (int k = 0; k < xArr.length; k++) {
            visitGrid(i + xArr[k], j + yArr[k], grid);
        }
    }

}
